package com.yxym.object_.Exercise01;

public class EqualsExercise01 {
    public static void main(String[] args) {
        Person person = new Person("ee",20,'男');
        Person person2 = new Person("ee",20,'男');
        System.out.println(person.equals(person2));//T
    }
}
//判断两个Person对象的内容是否相等，如果两个对象的属性都一样返回true
class Person {
    private String name;
    private int age;
    private char gender;
//重写Object的equals的方法
public boolean equals(Object obj) {
    //类型判断
    if (obj instanceof Person) {//是Person才比较
         Person person= ((Person)obj);//进行向下转型
         return this.name.equals(person.name) &&
                 this.age == person.age && this.gender == person.gender;
    }
    else if(this == obj){
        //判断比较两个对象是同一个对象，则直接返回true
        return true;
    }
    //不是Person，则直接返回false
    return false;
}

    public Person(String name, int age, char gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public char getGender() {
        return gender;
    }

    public void setGender(char gender) {
        this.gender = gender;
    }
}